import { getToken, setToken, removeToken } from '@/utils/auth'

import { sysProfile, sysUser } from '@/api/login'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: ''
  },
  mutations: {
    SETTOKEN(state, token) {
      state.token = token
      setToken(token)
    },

    setUserInfo(state, value) {
      state.userInfo = value
    },
    // 退出删除token
    logout(state) {
      state.token = ''
      state.userInfo = ''
      removeToken()
    }
  },
  actions: {
    async getUserInfo({ commit }) {
      const res = await sysProfile()

      const res2 = await sysUser(res.data.userId)
      commit('setUserInfo', Object.assign({}, res.data, res2.data))
    }
  }
}
